package com.ykdl.member.kid.doctor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.base.GlobalContext;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.AskDoctorAdapter;
import com.ykdl.member.kid.adapters.TopCatalogDoctorAdapter;
import com.ykdl.member.kid.adapters.TopicSheetListAdapter;
import com.ykdl.member.kid.beans.AskDoctorBean;
import com.ykdl.member.kid.beans.AskDoctorBeanListBean;
import com.ykdl.member.kid.beans.DoctorsBean;
import com.ykdl.member.kid.beans.TopicSheetList;
import com.ykdl.member.kid.doctor.DoctorOnlineActivity.getDoctorQuestionsListTag;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;
/**
 * 精选医生问题列表
 * @author chenshuai
 */
public class CatalogsDoctorListActivity extends BaseScreen{

	private TopCatalogDoctorAdapter adapter ;
	private RefreshListView ask_doctor_list ;
	private View head = null;
	private AsyncImageView doctor_head;
	private TextView doctor_name,doctor_position,doctor_adrr,doctor_info;
	private String catalog_id ="";
	private String TAG = "CatalogsDoctorListActivity";
	private CatalogBean catalog;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("张大锤", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		catalog_id = this.getIntent().getStringExtra(KidAction.DOCTOR_CATALOG_ID);
		setContent(R.layout.catalogsdoctorlistactivity_xml);
		inint();
	}
	public void inint() {
		head = LayoutInflater.from(mContext).inflate(
				R.layout.eventdetail_head, null);
		adapter = new TopCatalogDoctorAdapter(this);
		doctor_head =  (AsyncImageView)head.findViewById(R.id.doctor_head);
		doctor_name =  (TextView)head.findViewById(R.id.doctor_name);
		doctor_position =  (TextView)head.findViewById(R.id.doctor_position);
		doctor_adrr =  (TextView)head.findViewById(R.id.doctor_adrr);
		doctor_info =  (TextView)head.findViewById(R.id.doctor_info);
		doctor_info.setVisibility(View.GONE);
		doctor_adrr.setVisibility(View.GONE);
		ask_doctor_list = (RefreshListView)findViewById(R.id.ask_doctor_list);
		ask_doctor_list.addHeaderView(head);
		findViewById(R.id.ask_thedoctor).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				go(ToAskDoctorActivity.class);
			}
		});
		ask_doctor_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				CatalogTopicItem bean= (CatalogTopicItem)adapter.getItem(arg2-1);
				Intent intent = new Intent(CatalogsDoctorListActivity.this,ProblemDetailsAcivity.class);
				intent.putExtra(KidAction.DOCTOR_TOPICI_DD, bean.getItem().getTopic_id()+"");
				CatalogsDoctorListActivity.this.startActivity(intent);
			}
		});
		initData();
		
	}
	private void initData() {
		showProgress("正在加载中,请稍等...");
		final String url =  GlobalContext.PROJECT_SERVER+KidConfig.BASE_TEST_SERVER + "catalogs/" + catalog_id;
		HttpBaseRequest request = Wxxr.getInstance().getRequest(url, null);
		TaskManager.startHttpRequest(request, new TopicSheetListTag(), TopicSheetList.class);
	}
	class TopicSheetListTag implements ITag{
		@Override
		public void refreshUI(Object t, DataState ds) {
			finishProgress();
			if(null != t && t instanceof TopicSheetList){
				TopicSheetList bean = (TopicSheetList)t;
				catalog = bean.getCatalog();
				if(catalog.getCatalogItems() != null && catalog.getCatalogItems().size() > 0){
					showData();
				}
			}
		}
		@Override
		public boolean refreshError(DataParseError parseError) {
			finishProgress();
			return false;
		}
		@Override
		public void refreshCancel() {
		}
	}
	private void showData() {
		final ArrayList<CatalogTopicItem> list = new ArrayList<CatalogTopicItem>();
		CatalogItemBean item = null;
		for (int i = 0; i < catalog.getCatalogItems().size(); i++) {
			 item = catalog.getCatalogItems().get(i);
			if(item instanceof CatalogTopicItem){
				list.add((CatalogTopicItem)item );
			}
		}
		adapter.setList(list);
		setHeadData(catalog);
		ask_doctor_list.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}
	public void setHeadData(CatalogBean catalog) {
		if (catalog.getFile_meta()!=null) {
			String url = catalog.getFile_meta().getDownload_urls().getBig().getUrl();
			doctor_head.setUrl(TAG, url, ImageOptionsFactory.getSmallPortraitOptions(CatalogsDoctorListActivity.this));
		}
		doctor_name.setText(catalog.getTitle());
		doctor_position.setText(catalog.getTitle());
//		setTopNav(bean.getDisplay_name());
//		if (bean.getProfile()!=null) {
//			doctor_position.setText(bean.getProfile().getTitle());
//			doctor_adrr.setText(bean.getProfile().getDepartment());
//			doctor_info.setText(bean.getProfile().getIntro());
//		}
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
	

}
